package com.wysiwyg.admin.controller.flow;

import com.wysiwyg.admin.model.dto.UserChangeDTO;
import com.wysiwyg.admin.model.po.AdminUserChangePO;
import com.wysiwyg.admin.model.vo.UserChangeVO;
import com.wysiwyg.admin.service.UserChangeService;
import com.wysiwyg.common.web.response.ResponseEnum;
import com.wysiwyg.common.web.response.ServerResponseEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户变更流程控制器
 */
@Slf4j
@RestController
@RequestMapping("/flow/user-change")
@RequiredArgsConstructor
public class UserChangeFlowController {

    private final UserChangeService userChangeService;

    /**
     * 启动用户变更流程
     */
    @PostMapping("/startChangeFlow")
    public ServerResponseEntity<AdminUserChangePO> startChangeFlow(@RequestBody UserChangeDTO dto) {
        try {
            AdminUserChangePO result = userChangeService.startChangeFlow(dto);
            log.info("用户变更流程启动成功, changeNo={}", result.getChangeNo());
            return ServerResponseEntity.success(result);
        } catch (Exception e) {
            log.error("用户变更流程启动失败", e);
            return ServerResponseEntity.fail(ResponseEnum.BUSINESS_ERROR);
        }
    }

    /**
     * 根据用户ID查询变更记录
     */
    @GetMapping("/list/{userId}")
    public ServerResponseEntity<List<UserChangeVO>> getUserChangeList(@PathVariable String userId) {
        try {
            List<UserChangeVO> list = userChangeService.getBaseMapper().selectUserChangeList(userId);
            return ServerResponseEntity.success(list);
        } catch (Exception e) {
            log.error("查询用户变更记录失败, userId={}", userId, e);
            return ServerResponseEntity.fail(ResponseEnum.BUSINESS_ERROR);
        }
    }

    /**
     * 根据变更单号查询详情
     */
    @GetMapping("/detail/{changeNo}")
    public ServerResponseEntity<UserChangeVO> getUserChangeDetail(@PathVariable String changeNo) {
        try {
            UserChangeVO detail = userChangeService.getBaseMapper().selectUserChangeByNo(changeNo);
            if (detail == null) {
                return ServerResponseEntity.fail(ResponseEnum.NOT_FOUND);
            }
            return ServerResponseEntity.success(detail);
        } catch (Exception e) {
            log.error("查询变更详情失败, changeNo={}", changeNo, e);
            return ServerResponseEntity.fail(ResponseEnum.BUSINESS_ERROR);
        }
    }
}
